package com.group3.service.mqservice;

import com.group3.Feign.ProductServiceFeign;
import com.group3.Feign.UserServiceFeign;
import com.group3.common.topic.OrderTopic;
import com.group3.model.Order;
import com.group3.model.Product;
import com.group3.model.User;
import com.group3.service.OrderSevice;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RocketMQMessageListener(topic = OrderTopic.ORDER_ADD,consumerGroup = "addOrder")
public class AddOrderService implements RocketMQListener<Order> {
    @Autowired
    UserServiceFeign userServiceFeign;
    @Autowired
    ProductServiceFeign  productServiceFeign;
    @Autowired

    OrderSevice orderSevice;

    @Override
    public void onMessage(Order order) {
        Integer uid = order.getUid();
        Integer pid = order.getPid();
        if(uid!=null&&pid!=null){
           log.info("用户{}添加了商品{}的订单",uid,pid);
           User user=userServiceFeign.getUserByUid(uid);
           Product product=productServiceFeign.getProductByPid(pid);
           if (user!=null&& product!=null){
               order.setUid(user.getUid());
               order.setUsername(user.getUsername());
               order.setPid(product.getPid());
               order.setPname(product.getPname());
               order.setPprice(product.getPprice());
               order.setNumber(1);
               order.setStatus(0);
               orderSevice.addOrder(order);
               log.info("用户{}成功添加了商品{}的订单{}",uid,pid,order);
           }
        }
    }
}
